package site.eblog.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import site.eblog.dao.PermissionMapper;
import site.eblog.dao.RoleMapper;

import site.eblog.entity.Permission;
import site.eblog.entity.Role;

import site.eblog.dao.UserMapper;
import site.eblog.entity.User;

@Transactional
@Service
public class UserServiceImpl implements UserService {

	@Autowired UserMapper userMapper;
	@Autowired RoleMapper roleMapper;
	@Autowired PermissionMapper permissionMapper;
	
	@Override
	public User findByName(String loginName) {
		User user = new User();
		user = userMapper.findByName(loginName);
		return user;
	}
	@Override
	public Set<String> getRolesName(User user) {
		Set<String> resSet = new HashSet<String>();
		List<Role> roleList = roleMapper.getRolesById(user);
		for(Role role:roleList){
			resSet.add(role.getName());
		}
		return resSet;
	}

	@Override
	public List<Role> getRoleList(User user) {
		List<Role> roleList = roleMapper.getRolesById(user);
		return roleList;
	}

	@Override
	public List<String> getPermissionsName(Integer id) {
		List<Permission> permissions = permissionMapper.getPermissionById(id);
		List<String> permissionListName = new ArrayList<String>();
		for(Permission permission:permissions){
			permissionListName.add(permission.getName());
		}
		return permissionListName;
	}
	@Override
	public int insertUser(User user) {
		int res = userMapper.insertUser(user);
		return res;
	}
	@Override
	public int insertUserRole(User user) {
		int res = userMapper.insertUserRole(user);
		return res;
	}
	@Override
	public int editUserInfo(User user) {
		int res = userMapper.editUserInfo(user);
		return res;
	}
	@Override
	public List<User> getUsers() {
		List<User> users = new ArrayList<>();
		users = userMapper.getUsers();
		return users;
	}
	@Override
	public int delUser(String userName) {
		int res = userMapper.delUser(userName);
		return res;
	}
	@Override
	public int delUserRole(int userId) {
		int res = roleMapper.delUserRole(userId);
		return res;
	}
}
